package com.k8sops.backend.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.k8sops.backend.common.ApiResponse;
import com.k8sops.backend.entity.DOSProjectVersion;
import com.k8sops.backend.service.DOSProjectVersionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 项目版本控制器
 */
@RestController
@RequestMapping("/project-version")
public class DOSProjectVersionController {

    @Autowired
    private DOSProjectVersionService projectVersionService;

    /**
     * 添加项目版本
     */
    @PostMapping("/add")
    public ApiResponse<Boolean> addProjectVersion(@RequestBody DOSProjectVersion projectVersion) {
        boolean result = projectVersionService.addProjectVersion(projectVersion);
        return result ? ApiResponse.success(true) : ApiResponse.error("添加项目版本失败");
    }

    /**
     * 更新项目版本信息
     */
    @PutMapping("/update")
    public ApiResponse<Boolean> updateProjectVersion(@RequestBody DOSProjectVersion projectVersion) {
        if (projectVersion.getId() == null) {
            return ApiResponse.error("项目版本ID不能为空");
        }
        boolean result = projectVersionService.updateProjectVersion(projectVersion);
        return result ? ApiResponse.success(true) : ApiResponse.error("更新项目版本失败");
    }

    /**
     * 根据ID删除项目版本
     */
    @DeleteMapping("/delete/{id}")
    public ApiResponse<Boolean> deleteProjectVersion(@PathVariable Long id) {
        boolean result = projectVersionService.deleteProjectVersionById(id);
        return result ? ApiResponse.success(true) : ApiResponse.error("删除项目版本失败");
    }

    /**
     * 根据ID获取项目版本
     */
    @GetMapping("/get/{id}")
    public ApiResponse<DOSProjectVersion> getProjectVersion(@PathVariable Long id) {
        DOSProjectVersion projectVersion = projectVersionService.getProjectVersionById(id);
        return projectVersion != null ? ApiResponse.success(projectVersion) : ApiResponse.error("项目版本不存在");
    }

    /**
     * 分页查询项目版本
     */
    @GetMapping("/page")
    public ApiResponse<IPage<DOSProjectVersion>> pageProjectVersions(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) Integer projectId) {
        IPage<DOSProjectVersion> result = projectVersionService.pageProjectVersions(page, size, projectId);
        return ApiResponse.success(result);
    }
    
    /**
     * 获取项目最新版本
     */
    @GetMapping("/latest")
    public ApiResponse<DOSProjectVersion> getLatestProjectVersion(@RequestParam Integer projectId) {
        DOSProjectVersion latestVersion = projectVersionService.getLatestProjectVersion(projectId);
        return latestVersion != null ? ApiResponse.success(latestVersion) : ApiResponse.error("项目暂无版本");
    }
}
